package com.djk.utils;

import com.djk.pojo.AudioType;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.Tika;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

/**
 * 使用Tika检测文件格式
 */
@Slf4j
@Component
public class MusicTypeValidator {
    private static Tika tika = new Tika();
    @Autowired
    private AudioType audioType;

    /**
     * 遍历map，比对识别的文件的mime类型
     */
    public boolean isSupportMimeType(InputStream inputStream) throws IOException {
        String fileMimeType = tika.detect(inputStream);
//        log.info("文件为{}类型", fileMimeType);
        return audioType.isSupportType(fileMimeType);
    }

    //校验拓展名
    public boolean isSupportFileSuffix(String suffix) {
        return audioType.isSupportSuffix(suffix);
    }
}
